<template>
  <div>
    <div style="display: flex; align-items: center">
      产品编号:<el-input v-model="ProductCode" style="width: 200px"></el-input>
      产品名称:<el-input v-model="ProductName" style="width: 200px"></el-input>
      类型:<el-select v-model="ProductTypeId" placeholder="请选择">
        <el-option :value="0" label="请选择"> 请选择</el-option>
        <el-option :value="1" label="移动电源">移动电源 </el-option>
        <el-option :value="2" label="线材类">线材类 </el-option>
      </el-select>
      <el-button type="primary" @click="ProductManagementGetList()">查询</el-button>
      <el-button type="primary" @click="ProductAdd">添加</el-button>
      <el-button type="danger" @click="PLDelete()">批量删除</el-button>
      <el-button type="success" @click="exportListToExcel(multipleSelection)">导出</el-button>
    </div>

    <el-table ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: 100%"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column prop="productCode" label="产品编号"> </el-table-column>
      <el-table-column prop="productName" label="产品名称"> </el-table-column>
      <el-table-column prop="factoryNo" label="厂商编码"> </el-table-column>
      <el-table-column prop="innerNo" label="内部编码"> </el-table-column>
      <el-table-column prop="specs" label="规格"> </el-table-column>
      <el-table-column prop="productTypeId" label="类别">
        <template slot-scope="data">
          <span v-if="data.row.productTypeId == 1">移动电源</span>
          <span v-if="data.row.productTypeId == 2">线材类</span>
        </template>
      </el-table-column>
      <el-table-column prop="unit" label="存储单位"> </el-table-column>
      <el-table-column prop="floor" label="预警(下)"> </el-table-column>
      <el-table-column prop="ceiling" label="预警(上)"> </el-table-column>
      <el-table-column prop="inventory" label="库存数"> </el-table-column>
      <el-table-column prop="packageType" label="包装类型"> </el-table-column>
      <el-table-column prop="price" label="价格"> </el-table-column>
      <el-table-column prop="weight" label="重量"> </el-table-column>
      <el-table-column prop="defaultSupplierId" label="默认供应商">
        <template slot-scope="data">
          <span v-if="data.row.defaultSupplierId == true">华为科技</span>
          <span v-if="data.row.defaultSupplierId == false">小米科技</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="handleEdit(scope.row.id)">编辑</el-button>
          <el-button type="text" size="small" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <br />
    <div>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageIndex"
        :page-sizes="[5, 10, 15, 20]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
        :total="totalCount">
      </el-pagination>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageIndex"
        :page-sizes="[2, 3, 6, 10]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
        :total="totalCount">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import {
  ProductManagementGetList,
  DeleteProductManagementDtoAsyne,
  DeletesProductManagementIsDisable,
} from "@/api/BasicsDatumServer/ProductManagement";
import { autoGenerateCode } from "@/utils/utils";
import XLSX from "xlsx";
export default {
  data() {
    return {
      tableData: [],
      ProductCode: "",
      ProductName: "",
      ProductTypeId: 0,
      pageIndex: 1,
      pageSize: 5,
      totalCount: 0,
      pageCount: 0,
      multipleSelection: [],
    };
  },
  created() {
    this.ProductManagementGetList();
  },
  //计算属性
  computed: {
    sid() {
      var sid = [];
      this.multipleSelection.forEach((item) => {
        sid.push(item.id);
      });
      return sid;
    },
  },
  methods: {
    ProductManagementGetList() {
      var params = {
        ProductCode: this.ProductCode,
        ProductName: this.ProductName,
        ProductTypeId: this.ProductTypeId,
        pageIndex: this.pageIndex,
        pageSize: this.pageSize,
      };
      ProductManagementGetList(params).then((res) => {
        this.tableData = res.data.data.result;
        this.totalCount = res.data.data.totalCount;
        this.pageCount = res.data.data.pageCount;
      });
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.ProductManagementGetList();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageIndex = val;
      this.ProductManagementGetList();
    },
    //删除
    handleDelete(id) {
      this.$confirm("确定要删除吗", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          var myparms = {
            id: id,
          };
          DeleteProductManagementDtoAsyne(myparms).then((res) => {
            if (res.data.data > 0) {
              this.$message({
                message: "删除成功",
                type: "success",
              });
              this.ProductManagementGetList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //导出
    exportListToExcel(multipleSelection) {
      // 过滤需要导出的字段
      const filteredData = multipleSelection.map((item) => {
        return {
          产品编号: item.productCode,
          产品名称: item.productName,
          产品编码: item.factoryNo,
          内部编码: item.innerNo,
          规格: item.specs,
          类别: item.productTypeId == 1 ? "移动电源" : "线材类",
          存储单位: item.unit,
          预警下: item.floor,
          预警上: item.ceiling,
          库存数: item.inventory,
          包装类型: item.packageType,
          价格: item.price,
          重量: item.weight,
          默认供应商: item.defaultSupplierId == true ? "华为科技" : "小米科技",
          备注: item.remark,
        };
      });
      const worksheet = XLSX.utils.json_to_sheet(filteredData);
      //设置表格宽度
      worksheet["!cols"] = [
        { width: 10 },
        { width: 15 },
        { width: 15 },
        { width: 20 },
        { width: 15 },
      ];

      const workbook = XLSX.utils.book_new();
      XLSX.utils.book_append_sheet(workbook, worksheet, "Sheet1");
      // 导出Excel文件
      XLSX.writeFile(workbook, "设备类型.xlsx");
    },
    //批量删除获取列表
    handleSelectionChange(val) {
      this.multipleSelection = val;
      console.log(this.multipleSelection);
    },

    //批量删除方法
    PLDelete() {
      if (this.multipleSelection.length == 0) {
        this.$message({
          message: "请选择要删除的数据",
          type: "warning",
        });
        return;
      }

      this.$confirm("此操作将永久批量删除该文件?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          var myparams = {
            delId: this.sid.toString(),
          };
          console.log(myparams, 666666666);
          DeletesProductManagementIsDisable(myparams).then((res) => {
            if (res.data.code == 0) {
              this.$message({
                message: "批量删除成功",
                type: "success",
              });
              this.ProductManagementGetList();
            } else {
              this.$message({
                message: "批量删除失败",
                type: "success",
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消批量删除",
          });
        });
    },

    //跳转传值
    handleEdit(id) {
      this.$router.push({ path: "ProductManagementEdit", query: { id: id } });
    },
    ProductAdd() {
      this.$router.push("ProductManagementAdd");
    }
  },
};
</script>

<style></style>